import { Injectable,Inject } from '@angular/core';
import Swal from 'sweetalert2';
import { StorageService, SESSION_STORAGE } from 'ngx-webstorage-service';
import { CommonResponseVo } from '../model/common-response-vo';
@Injectable({
  providedIn: 'root'
})
export class UtilService {
  /**
   * 工具箱服务
   */
  constructor(@Inject(SESSION_STORAGE) private storage: StorageService) { }

  /**
   * 返回字母和数字组合的随机值
   * @param strlen 不能少6
   * @returns 小于6位或参数不可用返回null
   */
  public randomAlphaNumeric(strlen: number | null): string | null {
    if (!strlen || strlen < 6) {
      return null;
    }
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var randomstring = '';
    for (var i = 0; i < strlen; i++) {
      var rnum = Math.floor(Math.random() * chars.length);
      randomstring += chars.substring(rnum, rnum + 1);
    }
    return randomstring;
  }
  /**
   * 获得当前的unix时间戳
   */
  public getUnixStamp(): number {
    return Math.floor(Date.now() / 1000);
  }
  //npm install --save sweetalert2 @see://www.freakyjolly.com/angular-sweetalert2-tutorial/#.X9SPbdgzabg|//sweetalert2.github.io/
  /**
   * 弹出错误的提示窗口
   * @param message 错误内容
   */
  public alertError(message: string) {
    Swal.fire({
      position: 'center',
      icon: 'error',
      title: '',
      text: message,
      showConfirmButton: false,
      timer: 1500
    });
  }
  /**
   * 弹出成功的提示窗口
   * @param message 成功内容
   */
  public alertSuccess(message: string) {
    Swal.fire({
      position: 'center',
      icon: 'success',
      title: '',
      text: message,
      showConfirmButton: false,
      timer: 1500
    });
  }
  /**
   * 从版块地址(http://?/board/y-z.xhtml)中解析出相关主键(y|z)
   * @param boardRefer 
   * @returns 若参数不可用返回空数组,若分析失败返回空数组
   *          若返回的数组的数量(length)等于2,并且值都大于0表示成功([0]表示版块ID,[1]表示版块组ID)
   */
  public parseBoardDocumentReferrer(boardRefer: string): number[] {
    if (typeof boardRefer != 'undefined' && boardRefer) {
      let s = boardRefer.indexOf('/board/');
      let f = boardRefer.indexOf('.xhtml');
      let constr: string[] = boardRefer.substring(s + 7, f).split('-');
      if (constr.length != 2) {
        return [];
      }
      return constr.map((ele: string) => parseInt(ele) ?? 0);
    }
    return [];
  }
  /**
   * 从话题地址(http://?/topic/x-y-z.xhtml)中解析出相关主键(x|y|z)
   * @param topicRefer 
   * @returns 若参数不可用返回空数组,若分析失败返回空数组
   *          若返回的数组的数量(length)等于3,并且值都大于0表示成功([0]表示话题ID,[1]表示版块ID,[2]表示版块组ID)
   */
  public parseTopicDocumentReferrer(topicRefer: string): number[] {
    if (typeof topicRefer != 'undefined' && topicRefer) {
      let s = topicRefer.indexOf('/topic/');
      let f = topicRefer.indexOf('.xhtml');
      let constr: string[] = topicRefer.substring(s + 7, f).split('-');
      if (constr.length != 3) {
        return [];
      }
      return constr.map((ele: string) => parseInt(ele) ?? 0);
    }
    return [];
  }
  /**
   * 缓存举报类型
   * @param rs 
   */
  public cacheReportCategory(rs: CommonResponseVo[]): void{
    this.storage.set('cm-reportype', rs);
  }
  /**
   * 返回缓存的举报类型
   * @returns  不存在缓存时返回空数组
   */
  public getCacheReportCategory(): CommonResponseVo[]{
    if (!this.storage.has('cm-reportype')) {
      return [];
    }
    let rs:CommonResponseVo[] = this.storage.get('cm-reportype');
    return rs;
  }
  /**
   * 缓存解码的会员头像
   * @param memberId 会员头像 
   * @param avatarFilePath 解码的会员头像路径
   */
  public cacheDecodeMemberAvatar(memberId: number, avatarFilePath: string | null): void{
    if(0>=memberId){
      return;
    }
    if(null === avatarFilePath || !avatarFilePath){
      return;
    }
    const key: string = 'member#'+memberId+'-avatar'; 
    this.storage.set(key, avatarFilePath);
  }
  /**
   * 返回缓存的解码会员头像
   * @param memberId 会员头像 
   * @returns  不存在缓存时null
   */
  public getCacheDecodeMemberAvatar(memberId: number): string | null{
    if(0>=memberId){
      return null;
    }
    const key: string = 'member#'+memberId+'-avatar'; 
    if (!this.storage.has(key)) {
      return null;
    }
    let rs:string = this.storage.get(key);
    return rs;

  }
}
